package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import model.Warehouse;
import util.DBUtil;
public class WarehouseDAO {
	public Warehouse getByProductId(String productId){
		DBUtil util = new DBUtil("select * from warehouse where PRODUC_ID = ?");
		Warehouse wh = null;
		try {
			util.pst.setString(1, productId);
			ResultSet rs = util.pst.executeQuery();
			if(rs.next()){
				wh = new Warehouse(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getDate(5));
			}
			util.close();
			return wh;
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
			return null;
		}
	}
	public boolean save(Warehouse wr){
		DBUtil util = new DBUtil("insert into warehouse(PRODUC_ID,TATOL_QUANTITY,ENABLE_QUANTITY,UPDATE_TS) values(?,?,?,?)");
		try {
			util.pst.setString(1, wr.getProductId());
			util.pst.setString(2, wr.getTotalQunatity()+"");
			util.pst.setString(3, wr.getEnableQuantity()+"");
			util.pst.setString(4, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wr.getUpdateTS()));
			util.pst.execute();
			util.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
			return false;
		}
	}
	public boolean updateEnableQuantity(int enableQuantity,String productId){
		DBUtil util = new DBUtil("update warehouse set ENABLE_QUANTITY = ? where PRODUC_ID=?");
		try {
			util.pst.setString(1, enableQuantity+"");
			util.pst.setString(2, productId);
			util.pst.execute();
			util.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
			return false;
		}
	}
	public boolean updateTotalQuantity(int totalQuantity,String productId){
		DBUtil util = new DBUtil("update warehouse set TATOL_QUANTITY = ? where PRODUC_ID=?");
		try {
			util.pst.setString(1, totalQuantity+"");
			util.pst.setString(2, productId);
			util.pst.execute();
			util.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
			return false;
		}
	}
}
